<template>
	<view class="box" @click="hhh">
		<view class="business_ct cts">
			<view class="addressData" v-if="addressId" @click="onShowAddress()">
				<view class="addressCt">
					<view class="dwicon">

					</view>
					<view class="address_ct">
						{{addressInfo.address}}
					</view>
					<view class="arrowicon">

					</view>
				</view>
				<view class="userInfo">
					<span class="nickName">{{addressInfo.contact_person}}</span> <span
						class="phone">{{addressInfo.contact_phone}}</span>
				</view>
			</view>
			<view class="addressAdd" v-else @click="onNewAddress()">+点击添加地址</view>
		</view>
		<!-- 商品全部信息 -->
		<view class="goodsInfo" v-for="(item,index) in goodsInfo" :key="index">
			
			<!-- 商品信息 -->
			<view class="goodsItem_box">
				<view class="item">
					<view class="left">
						<image :src="item.groupbuy_image[0]" mode="widthFix" style="width: 220rpx; height: 220rpx;"></image>
					</view>
					<view class="right">
						<view class="goodsName">
							{{item.goods_name}}
						</view>
						<view class="total_price">
							<view class="price">
								<span class="icon">拼团价</span><span>￥</span><span class="ct">{{item.groupbuy_price}}</span>
							</view>
							<!-- <u-number-box v-model="goodsInfo[index].count" @change="valChange"></u-number-box> -->
						</view>
						<view class="people" style="font-size: 24rpx; color: #666;margin-top: 10rpx;">
							{{item.buy_min_number}}人成团
						</view>
					</view>
				</view>
			</view>
			<!-- 价格信息 -->
			<view class="PriceInfo">

				<view class="send_price">
					<span>配送费</span> <span style="color: #FA453C;">+￥{{item.fee}}</span>
				</view>
				<view class="computer_price">
					<span style="font-size: 32rpx; font-weight: 700;">￥{{computerPrices}}</span> <span>合计：</span>
					<span>共{{goodsInfo.length}}商品</span>
				</view>
			</view>
		</view>
		<!-- 订单备注 -->
		<view class="remark">
			<view class="title">
				订单备注（选填）
			</view>
			<view class="ct_area">
				<textarea placeholder="请输入备注信息" v-model="remark" />
			</view>
		</view>
		<!-- 提交 -->
		<view class="submit">
			<view class="subCt">
				<span>实付</span><span class="price">￥{{computerPrices}}</span>
			</view>
			<view class="subBtn" @click="()=>this.show = true">
				提交订单
			</view>
		</view>

		<!-- 支付方式 -->
		<u-popup v-model="show" mode="bottom" :closeable="true" border-radius="28">
			<view class="popBox">
				<view class="total_price">
					<span class="icon">￥</span>{{computerPrices}}
				</view>
				<view class="payItemView">
					<view class="payTypeTitme">请选择支付方式</view>
					<view class="payTypeItem" @click="onSelecteds(1)">
						<view class="left">
							<u-icon name="/static/weixin.png" size="50"></u-icon>
							<view class="payTypeTitle">微信支付</view>
						</view>

						<view class="right">
							<u-icon name="/static/selected.png" size="40" v-if="currents == 1"></u-icon>
							<u-icon name="/static/unselected2.png" size="40" v-else></u-icon>
						</view>
					</view>
				</view>
				<view class="pay" @click="onOrderPayment">
					立即支付
				</view>
			</view>
		</u-popup>
		<addressPopup ref="address" @select="onSelected" @edit="onEditAddress" @add="onNewAddress"></addressPopup>
	</view>
</template>
<script>
	import {
		// 默认收货地址
		getDefaultAddresses,
		// // 创建商品订单post
		// addOrder,
		// // 优惠券
		// getMyCoupons,
		// // 门店
		// getStorechain

	} from '@/api/user.js'
	import {startPt} from '/common/group.js'
	import addressPopup from "@/components/address-popup.vue"
	import toast from "../../uni_modules/vk-uview-ui/libs/function/toast.js";
	import config from '@/config/config.js'
	export default {
		components: {
			addressPopup
		},
		data() {
			return {
				tabs: [{
					id: 1,
					title: '商家配送',
				}, {
					id: 2,
					title: '到店自提',
				}, ],
				//设置tab下标
				current: 3,
				//商品信息
				goodsInfo: [],
				
				//总价
				totalPrice: '0',
				
				//提交订单的价格
				computerPrices: '0',
				//自提价格
				zitiPrice: '0',
				//备注
				remark: '',
				// 支付方式
				currents: '',
				//支付显示框
				show: false,
				style: {},
				// 收货地址
				addressInfo: '',
				// 收货地址id
				addressId: "",
				// 门店地址
				chain_addressInfo: "",
				//门店id
				chain_id: "",
				// 门店列表
				chain_list: "",
				//打开门店
				shopshow: false,
				// 第几个门店
				value: 0,
				// 自提点id
				zitiId: "",
				// 自提人信息
				userInfo: {
					name: "",
					phone: ""
				},
				type:1,


			}
		},
		onPageScroll(e) {
			if (e.scrollTop > 157) {
				this.style = {
					background: '#6dc762'
				}
			} else {
				this.style = {
					background: 'transparent'
				}
			}
		},
		onLoad(res) {
			console.log(res);
			this.initData()
			console.log("查看信息", JSON.parse(res.goods));
			this.goodsInfo = JSON.parse(res.goods)
			this.computerPrice()
			if(res.type){
				console.log("type",res.type);
				this.type = res.type
			}
		},
		methods: {
			initData() {
				getDefaultAddresses().then((res) => {
					console.log("默认地址数据", res)
					if (res.data) {
						this.addressInfo = res.data;
						this.addressId = res.data.id;
					}
				})

				// getMyCoupons().then((res) => {
				// 	console.log("我的优惠券数据", res)
				// })
				// getStorechain().then((res) => {
				// 	console.log("我的门店数据", res)
				// 	if (res.data) {
				// 		this.chain_list = res.data;
				// 	}
				// })
			},
		
			//商品数量
			valChange(index) {
				console.log("hhh", index);
				this.computerPrice()

			},
			//打开指定的下拉框
			selectType(id) {
				if (this.optionId == id) {
					this.optionId = 3
					return
				}
				this.optionId = id
			},
			//点击取消下拉框(规格)
			hhh() {
				this.optionId = 3
			},
			
			// 打开所有地址
			onShowAddress() {
				this.$refs.address.open();
			},
			//计算总价
			computerPrice() {
				let total = 0
				for (let i = 0; i < this.goodsInfo.length; i++) {
					total += Number(this.goodsInfo[i].groupbuy_price)
				}
				this.totalPrice = total
				this.computerPrices = (this.totalPrice + Number(this.goodsInfo[0].fee)).toFixed(2)
				this.zitiPrice = (this.totalPrice + Number(this.goodsInfo[0].fee)).toFixed(2)
			},
			//选择支付方式
			onSelecteds(i) {
				this.currents = i
			},
			onOrderPayment() {
				let info = this.goodsInfo[0]
				console.log(info);
				if(!this.currents){
					toast("请选择支付方式")
					return
				}

				if (this.current == 3) {
					if (this.goodsInfo.length == 0) {
						toast('未获取到商品信息')
						return
					}
					if (this.addressId == '' || this.addressId == null) {
						toast('请选择收货地址')
						return
					}
					if(this.type==1){
						console.log("我是发起");
					}else{
						console.log("我是参与");
					}
					let obj = {
						sgo_id:info.sgo_id,
						sgo_sid:info.groupbuy_id,
						sgo_storeid:info.store_id,
						sgo_goodsid:info.goods_id,
						sgo_number:1,
						sgo_price:info.groupbuy_price,
						sgo_amount:this.computerPrices,
						sgo_charge:info.charge,
						sgo_fee:info.fee,
						sgo_address:this.addressId,
						sgo_message:this.remark,
						sgo_type:this.type
						
					}

					console.log('查看', obj);
					startPt(obj).then(res => {
						if(!res.data.flag){
							uni.showToast({
								title:res.data.msg,
								icon:"none"
							})
							return
						}
						uni.redirectTo({
							url: '/pages/payment/payment?order_no=' +
								res.data.order_sn + '&money=' + this.computerPrices
						});
					})
				}
			},
			miniPayment() {
				let self = this;
				self.$user.orderMiniPayment({
					order_no: this.orderNo,
				}, true).then(res => {
					self.onPayment("wxpay", res.data.pay_params);
				});
			},
			onPayment(provider, orderInfo) {
				let self = this;
				self.$utils.pay(provider, orderInfo, (res) => {
					self.$utils.tips('支付成功');
					setTimeout(function() {
						uni.navigateBack({
							delta: 1
						});
					}, 1500);
				}, (res) => {
					self.$utils.tips('支付失败');
				}, (res) => {
					console.log("==============" + JSON.stringify(res, null, 2));
				});
			},
			// 选择地址,添加,编辑
			onNewAddress() {
				// this.$utils.jump('/other/add-address/add-address?type=0');
				uni.navigateTo({
					url: '/other/add-address/add-address?type=0'
				})
			},
			onSelected(item) {
				this.addressInfo = item;
				this.addressId = item.id;
			},
			onEditAddress(item) {
				// this.$utils.jump(');
				uni.navigateTo({
					url: '/other/add-address/add-address?type=1&id=' + item.id
				})
			},
			// 选择门店
			radioGroupShopChange(item) {
				console.log("门店信息", item);
				this.chain_addressInfo = item
				this.zitiId = item.chain_id
				this.addressId = item.area_id
			}
		}
	}
</script>
<style>
	page {
		background: #f5f5f5;
	}
</style>
<style lang="scss" scoped>
	.box {
		position: relative;
		background-color: #f5f5f5;
		width: 100vw;
		padding: 1rpx;
		box-sizing: border-box;
	}


	.cts {
		width: 710rpx;
		margin: 20rpx 20rpx;
		border-radius: 20rpx;
		background-color: #ffffff;
		box-sizing: border-box;
		padding: 30rpx 20rpx;

		.addressCt {
			display: flex;
			align-items: flex-start;

			.dwicon {
				margin: 14rpx 18rpx 0 0;
				width: 32rpx;
				height: 32rpx;
				background-image: url('/static/img/dw1.png');
				background-size: 32rpx 32rpx;
				background-repeat: no-repeat;
			}

			.address_ct {
				width: 580rpx;
				font-size: 32rpx;
				height: 120rpx;
				line-height: 60rpx;
				font-weight: 400;
				color: #333333;
			}

			.arrowicon {
				margin: 14rpx 0 0 18rpx;
				width: 32rpx;
				height: 32rpx;
				background-image: url('/static/img/arrow.png');
				background-size: cover;
				background-repeat: no-repeat;
			}
		}

		.userInfo {
			font-size: 28rpx;
			font-weight: 400;
			color: #333333;

			.nickName {
				margin-right: 20rpx;
			}
		}


	}




	// 商品信息
	.goodsInfo {
		width: 710rpx;
		box-sizing: border-box;
		padding: 30rpx 20rpx;
		margin: 40rpx auto;
		border-radius: 18rpx;
		background-color: #ffffff;
		font-family: SourceHanSansCN-Regular;

		//商品信息
		.goodsItem_box {
			margin-top: 30rpx;
			padding: 0 0 20rpx 0;
			border-bottom: 1rpx solid #f0f1f2;

			.item {
				display: flex;
				margin-top: 20rpx;

			}

			.item .left {
				width: 220rpx;
				height: 220rpx;
				border-radius: 18rpx;
				overflow: hidden;
				margin-right: 20rpx;
			}
			.item .right{
				display: flex;
				flex-direction: column;
				justify-content: space-between;
			}

			.item .right .goodsName {
				width: 480rpx;
				font-size: 26rpx;
				font-weight: 400;
				color: #333333;
				margin-bottom: 20rpx;
			}

			.item .right .total_price {
				display: flex;
				justify-content: space-between;
				align-items: center;
				font-size: 20rpx;
				color: #FA453C;

				.ct {
					font-size: 36rpx;
				}
				.icon{
					display: inline-block;
					width: 80rpx;
					height: 36rpx;
					background: #F5E7E6;
					border-radius: 10rpx;
					text-align: center;
					line-height: 36rpx;
					color: #FA453CFF;
					padding: 5rpx;
					margin-right: 10rpx;
				}
			}

			.item .right .select {
				width: 116rpx;
				height: 40rpx;
				background: #F5F5F5;
				border-radius: 20rpx;
				line-height: 40rpx;
				text-align: center;
				font-size: 24rpx;
				color: #666;
				position: relative;
			}

			.types {
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.item .right .select .types .icon {
				display: inline-block;
				width: 20rpx;
				height: 20rpx;
				background-image: url('/static/向下.png');
				background-size: cover;
				background-repeat: no-repeat;
			}

			.typeCt {
				height: 0rpx;
				background-color: #333333;
				color: white;
				overflow: hidden;
				position: absolute;
				top: 40rpx;
				left: 10rpx;
			}

			.typeCt.active {
				border-radius: 10rpx;
				text-align: center;
				width: 100rpx;
				height: 120rpx;
				transition: all linear 0.5s;
				z-index: 999;
			}
		}


	}

	//价格信息
	.PriceInfo {
		.cupons_price .icon {
			display: inline-block;
			width: 28rpx;
			height: 28rpx;
			background-image: url('/static/img/arrow.png');
			background-size: cover;
			background-repeat: no-repeat;
			margin: 0 0 0 8rpx;
		}

		.cupons_price,
		.send_price,
		.total_price {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 16rpx 0;
			font-size: 28rpx;
			font-family: Source Han Sans CN;
			font-weight: 400;
			color: #333333;
			border-bottom: 1px solid #f0f1f2;
		}

		.computer_price {
			display: flex;
			flex-direction: row-reverse;
			font-size: 28rpx;
			font-family: Source Han Sans CN;
			align-items: center;
			font-weight: 400;
			color: #333333;
			margin-top: 10rpx;
			padding-top: 10rpx;

			span {
				margin-right: 12rpx;
			}
		}

	}

	//备注
	.remark {
		width: 710rpx;
		margin: 0 auto;
		box-sizing: border-box;
		padding: 30rpx 20rpx;
		border-radius: 28rpx;
		background-color: #fff;

		.title {
			font-size: 28rpx;
			font-family: Source Han Sans CN;
			font-weight: 400;
			color: #333333;
		}

		.ct_area {
			margin-top: 20rpx;
		}
	}

	// 提交
	.submit {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 750rpx;
		box-sizing: border-box;
		height: 96rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 1rpx 10rpx 0rpx rgba(153, 153, 153, 0.2);
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 30rpx;
		padding: 0 20rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #333333;

		.subCt .price {
			color: #FA453C;
			font-size: 36rpx;
		}

		.subBtn {
			width: 200rpx;
			height: 70rpx;
			background: #fc562f;
			border-radius: 35rpx;
			text-align: center;
			line-height: 70rpx;
			color: #fff;
		}

	}

	// 支付方式
	.popBox {
		width: 750rpx;
		box-sizing: border-box;
		padding: 30rpx 20rpx;
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 0rpx 0rpx;
		height: 720rpx;
		position: relative;

		.total_price {
			margin-top: 40rpx;
			text-align: center;
			font-size: 56rpx;
			font-family: Source Han Sans CN;
			font-weight: 500;
			color: #333333;

			.icon {
				font-size: 36rpx;
			}
		}

		.pay {
			position: absolute;
			bottom: 40rpx;
			left: 30rpx;
			width: 690rpx;
			height: 80rpx;
			background: #fc562f;
			border-radius: 40rpx;
			text-align: center;
			line-height: 80rpx;
			color: #fff;
		}

	}

	.addressAdd {
		color: #F6632D;
		text-align: center;
		display: flex;
		justify-content: center;
		font-size: 30rpx;
	}

	.payItemView .payTypeItem {
		display: flex;
		justify-content: space-between;
		margin: 20rpx 0;
	}

	.left {
		display: flex;
		width: 180rpx;
		justify-content: space-between;
		align-items: center;
	}
</style>